<?php
namespace Admin\Controller;

use Think\Upload;

/**
 * 文件上传
 * 
 * @author Administrator
 *        
 */
class FileController extends CommController {
	
	// 处理uploadify上传的图片
	public function uploadify() {
		$upload = new Upload ();
		$upload->rootPath = C ( 'UPLOAD_PATH' );
		$upload->maxSize = 1048579;
		$info = $upload->upload ();
		$return = returnArray ();
		if ($info) {
			$return ['info'] = __ROOT__ . '/Uploads/' . $info ['Filedata'] ['savepath'] . $info ['Filedata'] ['savename'];
			$return ['tempname'] = $_FILES ["Filedata"] ["name"];
		} else {
			$return ['info'] = $upload->getError ();
			$return ['status'] = 0;
		}
		$this->ajaxReturn ( $return );
	}
	//处理上传的视频，音频，只能单视频单视频上传
    public function uploadvideo() {
		$upload = new Upload ();
		$upload->rootPath = C ( 'UPLOAD_PATH' );
		$upload->maxSize = 50048579;
		$info = $upload->upload ();
		$return = returnArray ();
                $key = key($_FILES);
		if ($info) {
			$return ['info'] = __ROOT__ . '/Uploads/' . $info [$key] ['savepath'] . $info [$key] ['savename'];
			$return ['tempname'] = $_FILES [$key] ["name"];
		} else {
			$return ['info'] = $upload->getError ();
			$return ['status'] = 0;
		}
		echo json_encode($return) ;
	}
	// 删除图片
	public function removeImage() {
		if (empty ( $_POST ['img'] )) {
			die ( $this->returnValidate ( "非法操作", "n" ) );
		} else {
			$root = __ROOT__ . '/';
			$img = str_replace ( $root, '', $_POST ['img'] );
			if (file_exists ( $img )) {
				if (unlink ( $img )) {
					die ( $this->returnValidate ( "删除成功", "y" ) );
				} else {
					die ( $this->returnValidate ( "删除失败", "n" ) );
				}
			} else {
				die ( $this->returnValidate ( "此图片不存在", "n" ) );
			}
		}
	}

	/**
	 * 返回json数据
	 *
	 * @param string $info
	 * @param string $status
	 * @param string $url
	 * @return string
	 */
	private function returnValidate($info = "未知错误", $status = "n", $url = "") {
		$array = array ();
		$array ['info'] = $info;
		$array ['status'] = $status;
		$array ['url'] = $url;
		return json_encode ( $array );
	}
}

?>